package Attributes;

import org.apache.commons.lang.WordUtils;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

/**
 *
 * @author Alexander
 */
public class AttributeCleaner {

    static CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1

    public static String cleanAttribute(String type, String attribute) {
        System.out.println("AttributeCleaner.cleanAttribute(" + type + "," + attribute + ")");
        if (!isPureAscii(attribute) || attribute.toLowerCase().contains("feat") || attribute.toLowerCase().contains("featuring") || attribute.toLowerCase().contains("ft")|| attribute.toLowerCase().contains("&") ||  attribute.toLowerCase().contains(",")) {
            if (type.equals("Artist")) {
                attribute = cleanArtist(attribute);
            } else if (type.equals("Title")) {
                attribute = cleanTitle(attribute);
            }
        }
        return attribute.trim();
    }

    public static boolean isPureAscii(String v) {
        System.out.println("AttributeCleaner.isPureAscii(" + v + ")");
        return asciiEncoder.canEncode(v);
    }

        public static String cleanArtist(String artist) {
        System.out.println("AttributeCleaner.cleanArtist(" + artist + ")");
        artist = artist.toLowerCase().replaceAll("/*", "").replaceAll("å", "a").replaceAll("'", "").replaceAll("-", " ").replace("á", "a").replaceAll("é", "e").replaceAll("í", "i").replace("ó", "o").replaceAll("ú", "u").replaceAll("ü", "u").replaceAll("ñ", "n");
        try {
            artist = artist.substring(0, artist.indexOf("featuring")).replace("(", "").replace("[", "");;
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf(" with "));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("feat"));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("feat."));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("ft."));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("vs"));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("vs."));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf("&"));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            artist = artist.substring(0, artist.indexOf(","));
            artist = artist.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        if((!artist.contains("("))&&(artist.contains(")"))){
            artist = artist.replace(")", "");
        }
        return capitalize(artist).trim();
    }

    public static String cleanTitle(String title) {
        System.out.println("AttributeCleaner.cleanTitle(" + title + ")");

        title = title.toLowerCase().replaceFirst(":", "").replaceFirst(",", "").replace("á", "a").replaceAll("é", "e").replaceAll("í", "i").replace("ó", "o").replaceAll("ú", "u").replaceAll("ü", "u").replaceAll("ñ", "n").replaceAll("'", "");
        try {
            title = title.substring(0, title.indexOf("feat"));
            title = title.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            title = title.substring(0, title.indexOf("feat."));
            title = title.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        try {
            title = title.substring(0, title.indexOf("ft."));
            title = title.replace("(", "").replace("[", "");
        } catch (Exception ex) {
        }
        title = capitalize(title).trim();
        if((!title.contains("("))&&(title.contains(")"))){
            title = title.replace(")", "");
        }
        return title;
    }
    public static String clean(String word) {
        System.out.println("AttributeCleaner.clean(" + word + ")");
        return word.replaceAll("〜", "").replace("'", "").replaceAll(";", "");
    }

    public static String capitalize(String source) {
        System.out.println("AttributeCleaner.capitalize(" + source + ")");
        String text = WordUtils.capitalize(source);
        return text.trim();
    }
}
